package water;

import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.items.ItemHandlerHelper;

import javax.annotation.Nullable;

/*import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;

import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.GameType;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.client.Minecraft;

import javax.annotation.Nullable;*/
@Mod.EventBusSubscriber
public class handler {
    @SubscribeEvent
    public static void onRightClickItem(PlayerInteractEvent.RightClickItem event) {
        Player entity = event.getPlayer();
        if (event.getHand() != entity.getUsedItemHand())
            return;
        execute(event, event.getWorld(), entity);
    }

    public static void execute(LevelAccessor world, Entity entity) {
        execute(null, world, entity);
    }

    private static void execute(@Nullable Event event, LevelAccessor world, Entity entity) {
        if (entity == null)
            return;
        if ((entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY).getItem() == Items.BUCKET) {
            if ((world.getBlockState(new BlockPos(
                    (int) (entity.level
                            .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                    ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                            .getBlockPos().getX()),
                    (int) (entity.level
                            .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                    ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                            .getBlockPos().getY()),
                    (int) (entity.level
                            .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                    ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                            .getBlockPos().getZ())))).getBlock() instanceof LiquidBlock) {
                if (!(world.getBlockState(new BlockPos(
                        (int) (entity.level
                                .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                        ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                                .getBlockPos().getX()),
                        (int) (entity.level
                                .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                        ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                                .getBlockPos().getY()),
                        (int) (entity.level
                                .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                        ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                                .getBlockPos().getZ())))).getFluidState().isSource()) {
                    if (!(new Object() {
                        public boolean checkGamemode(Entity _ent) {
                            if (_ent instanceof ServerPlayer _serverPlayer) {
                                return _serverPlayer.gameMode.getGameModeForPlayer() == GameType.CREATIVE;
                            } else if (_ent.level.isClientSide() && _ent instanceof Player _player) {
                                return Minecraft.getInstance().getConnection().getPlayerInfo(_player.getGameProfile().getId()) != null
                                        && Minecraft.getInstance().getConnection().getPlayerInfo(_player.getGameProfile().getId())
                                        .getGameMode() == GameType.CREATIVE;
                            }
                            return false;
                        }
                    }.checkGamemode(entity))) {
                        ((entity instanceof LivingEntity _livEnt ? _livEnt.getMainHandItem() : ItemStack.EMPTY)).shrink(1);
                        if (entity instanceof Player _player) {
                            ItemStack _setstack = ((world.getFluidState(new BlockPos(
                                            (int) (entity.level.clip(new ClipContext(entity.getEyePosition(1f),
                                                    entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                                    ClipContext.Fluid.ANY, entity)).getBlockPos().getX()),
                                            (int) (entity.level.clip(new ClipContext(entity.getEyePosition(1f),
                                                    entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                                    ClipContext.Fluid.ANY, entity)).getBlockPos().getY()),
                                            (int) (entity.level.clip(new ClipContext(entity.getEyePosition(1f),
                                                    entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                                    ClipContext.Fluid.ANY, entity)).getBlockPos().getZ())))
                                    .createLegacyBlock()).getBlock()instanceof LiquidBlock _liquid
                                    ? new ItemStack(_liquid.getFluid().getBucket())
                                    : ItemStack.EMPTY);
                            _setstack.setCount(1);
                            ItemHandlerHelper.giveItemToPlayer(_player, _setstack);
                        }
                    }
                    world.setBlock(
                            new BlockPos(
                                    (int) (entity.level.clip(new ClipContext(
                                            entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                            ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity)).getBlockPos().getX()),
                                    (int) (entity.level
                                            .clip(new ClipContext(entity.getEyePosition(1f),
                                                    entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                                    ClipContext.Fluid.ANY, entity))
                                            .getBlockPos().getY()),
                                    (int) (entity.level.clip(new ClipContext(entity.getEyePosition(1f),
                                            entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                            ClipContext.Fluid.ANY, entity)).getBlockPos().getZ())),
                            Blocks.AIR.defaultBlockState(), 3);
                }
            }
        }
        if ((entity instanceof LivingEntity _livEnt ? _livEnt.getOffhandItem() : ItemStack.EMPTY).getItem() == Items.BUCKET) {
            if ((world.getBlockState(new BlockPos(
                    (int) (entity.level
                            .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                    ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                            .getBlockPos().getX()),
                    (int) (entity.level
                            .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                    ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                            .getBlockPos().getY()),
                    (int) (entity.level
                            .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                    ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                            .getBlockPos().getZ())))).getBlock() instanceof LiquidBlock) {
                if (!(world.getBlockState(new BlockPos(
                        (int) (entity.level
                                .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                        ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                                .getBlockPos().getX()),
                        (int) (entity.level
                                .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                        ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                                .getBlockPos().getY()),
                        (int) (entity.level
                                .clip(new ClipContext(entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                        ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity))
                                .getBlockPos().getZ())))).getFluidState().isSource()) {
                    if (!(new Object() {
                        public boolean checkGamemode(Entity _ent) {
                            if (_ent instanceof ServerPlayer _serverPlayer) {
                                return _serverPlayer.gameMode.getGameModeForPlayer() == GameType.CREATIVE;
                            } else if (_ent.level.isClientSide() && _ent instanceof Player _player) {
                                return Minecraft.getInstance().getConnection().getPlayerInfo(_player.getGameProfile().getId()) != null
                                        && Minecraft.getInstance().getConnection().getPlayerInfo(_player.getGameProfile().getId())
                                        .getGameMode() == GameType.CREATIVE;
                            }
                            return false;
                        }
                    }.checkGamemode(entity))) {
                        ((entity instanceof LivingEntity _livEnt ? _livEnt.getOffhandItem() : ItemStack.EMPTY)).shrink(1);
                        if (entity instanceof Player _player) {
                            ItemStack _setstack = ((world.getFluidState(new BlockPos(
                                            (int) (entity.level.clip(new ClipContext(entity.getEyePosition(1f),
                                                    entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                                    ClipContext.Fluid.ANY, entity)).getBlockPos().getX()),
                                            (int) (entity.level.clip(new ClipContext(entity.getEyePosition(1f),
                                                    entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                                    ClipContext.Fluid.ANY, entity)).getBlockPos().getY()),
                                            (int) (entity.level.clip(new ClipContext(entity.getEyePosition(1f),
                                                    entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                                    ClipContext.Fluid.ANY, entity)).getBlockPos().getZ())))
                                    .createLegacyBlock()).getBlock()instanceof LiquidBlock _liquid
                                    ? new ItemStack(_liquid.getFluid().getBucket())
                                    : ItemStack.EMPTY);
                            _setstack.setCount(1);
                            ItemHandlerHelper.giveItemToPlayer(_player, _setstack);
                        }
                    }
                    world.setBlock(
                            new BlockPos(
                                    (int) (entity.level.clip(new ClipContext(
                                            entity.getEyePosition(1f), entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)),
                                            ClipContext.Block.OUTLINE, ClipContext.Fluid.ANY, entity)).getBlockPos().getX()),
                                    (int) (entity.level
                                            .clip(new ClipContext(entity.getEyePosition(1f),
                                                    entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                                    ClipContext.Fluid.ANY, entity))
                                            .getBlockPos().getY()),
                                    (int) (entity.level.clip(new ClipContext(entity.getEyePosition(1f),
                                            entity.getEyePosition(1f).add(entity.getViewVector(1f).scale(5)), ClipContext.Block.OUTLINE,
                                            ClipContext.Fluid.ANY, entity)).getBlockPos().getZ())),
                            Blocks.AIR.defaultBlockState(), 3);
                }
            }
        }
    }
}
